package com.xbongbong.saas.service;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.opportunityuser.pojo.dto.QuotationUserDeleteBatchDTO;
import com.xbongbong.pro.opportunityuser.pojo.vo.QuotationUserDeleteBatchVO;
import com.xbongbong.saas.domain.entity.QuotationUserEntity;
import com.xbongbong.saas.model.QuotationUserModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author long.rao
 * @version v1.0
 * @date 2019/8/16 20:52
 * @since v1.0
 */
@Service("quotationUserService")
public class QuotationUserServiceImpl implements QuotationUserService {
    private static final Logger LOG = LoggerFactory.getLogger(QuotationUserServiceImpl.class);

    @Resource
    private QuotationUserModel quotationUserModel;

    @Override
    public QuotationUserDeleteBatchVO deleteBatch(QuotationUserDeleteBatchDTO quotationUserDeleteBatchDTO) throws XbbException {
        QuotationUserDeleteBatchVO quotationUserDeleteBatchVO = new QuotationUserDeleteBatchVO();
        try {
            List<Long> quotationIdIn = quotationUserDeleteBatchDTO.getQuotationIdIn();
            String corpid = quotationUserDeleteBatchDTO.getCorpid();
            quotationUserModel.deleteBatchByQuotationIdIn(quotationIdIn, corpid);
        } catch (Exception e) {
            LOG.error("QuotationUserServiceImpl.deteleBatch 执行出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return quotationUserDeleteBatchVO;
    }

    @Override
    public void dataConsistencyUpdateUser(String corpid, String userId, String userName, String userAvatar) throws XbbException {
        try {
            Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            param.put("corpid", corpid);
            param.put("userId", userId);
            param.put("del", 0);
            List<QuotationUserEntity> quotationUserEntities = quotationUserModel.findEntitys(param);
            for (QuotationUserEntity entity : quotationUserEntities) {
                entity.setUserName(userName);
                entity.setUserAvatar(userAvatar);
                quotationUserModel.update(entity);
            }
        } catch (Exception e) {
            LOG.error("QuotationUserServiceImpl.dataConsistencyUpdateUser 出错，corpid=" + corpid + " userId=" + userId + "  userName=" + userName + "  userAvatar=" + userAvatar, e);
        }
    }
}
